package com.education.mymediaplayer;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class EasyPermissionActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private Button mButtonCamera;
    public static final int REQUEST_CAMERA_PERMISSON = 0x234;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy_permission);
        mButtonCamera = (Button) findViewById(R.id.button_camera);
        mButtonCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCamera();
            }
        });
    }

    @AfterPermissionGranted(REQUEST_CAMERA_PERMISSON)
    private void openCamera() {
        if (EasyPermissions.hasPermissions(EasyPermissionActivity.this, Manifest.permission.CAMERA)) {
            //拥有权限授权

            Toast.makeText(this, "拥有相机权限", Toast.LENGTH_SHORT).show();
        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(EasyPermissionActivity.this, Manifest.permission.CAMERA)) {
                openSettings();
            } else {
                //无授权
                Toast.makeText(this, "没有相机授权", Toast.LENGTH_SHORT).show();
                EasyPermissions.requestPermissions(this, "我要扫描二维码", REQUEST_CAMERA_PERMISSON, Manifest.permission.CAMERA);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        for (String perm : perms) {
            Log.d("--------", "--------" + perm);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        for (String perm : perms) {
            Log.d("--------", "--------" + perm);
        }
    }

    public void openSettings() {
        new AlertDialog.Builder(EasyPermissionActivity.this)
                .setMessage("必须同意该权限")
                .setNegativeButton("打开设置界面", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

//                                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION;
//                                Intent intent2 = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Intent intent2 = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent2.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent2);
                    }
                })
                .create()
                .show();
    }
}
